package org.yjc.controller;

import java.util.HashMap;
import java.util.HashSet;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yjc.custom.DatagridUtils;
import org.yjc.custom.JournalUtils;
import org.yjc.custom.UserUtils;
import org.yjc.entity.College;
import org.yjc.entity.SysUser;
import org.yjc.form.CollegeForm;
import org.yjc.repository.SysRoleRepository;
import org.yjc.service.CollegeService;

@Controller
@RequestMapping(value="/college")
public class CollegeControllet {
	@Autowired
	private CollegeService collegeservice;
	@Autowired
	SysRoleRepository sysroleDAO;
	@Autowired
	private UserUtils userutils;
	@Autowired
	private JournalUtils journalutils;
	
	@RequestMapping(value="/collegemain")
	public String collegemain() {
		return "college/collegemain";
	}
	
	@RequestMapping(value="/list")
	@ResponseBody
	public Object list(DatagridUtils param,String collegename,String principal) {
		Pageable pageable = param.buildPageable();
		Page<College> page = null;
		//安全查询
			Specification<College> spec = new Specification<College>() {
				@Override
				public Predicate toPredicate(Root<College> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
					HashSet<Predicate> rules = new HashSet<>();
					if(StringUtils.hasText(collegename)) {
						rules.add(criteriaBuilder.like(root.get("collegename"), "%"+collegename+"%"));
					}else if(StringUtils.hasText(principal)) {
						rules.add(criteriaBuilder.like(root.get("principal"), "%"+principal+"%"));
					}
					return criteriaBuilder.and(rules.toArray(new Predicate[rules.size()]));
				}
			};
			page = collegeservice.findAll(spec,pageable);
			HashMap<String, Object> result = DatagridUtils.biuldDatagridResult(page);
			return result;
	}
	
	@RequestMapping(value="/save")
	@ResponseBody
	public String save(CollegeForm form) {
		College college = new College();
		SysUser sysuser = userutils.getUser();
		if(form.getId()!=null) { //判断用户为新增或修改
			college = collegeservice.findById(form.getId());
			college.setId(form.getId());
			BeanUtils.copyProperties(form, college);
			//录入日志
			journalutils.journal1("修改学院信息",sysuser.getName()+"进行了修改学院信息操作");
		}else {
			//录入日志
			journalutils.journal1("添加学院",sysuser.getName()+"添加了"+"("+form.getCollegename()+")");
		}
		BeanUtils.copyProperties(form, college,"id");
		collegeservice.save(college);
		return "OK";
	}
	
	@RequestMapping(value="/delete")
	@ResponseBody
	public String delete(String ids) {
		SysUser sysuser = userutils.getUser();
		String[] ids1 = ids.split(",");//去除“，” 将多个id存入数组中
		for(int i = 0;i<ids1.length;i++) {
			College college = collegeservice.findById(Integer.parseInt(ids1[i]));
			collegeservice.deleteById(Integer.parseInt(ids1[i]));
			//日志
			journalutils.journal1("删除学院",sysuser.getName()+"删除了"+"("+college.getCollegename()+")");
		}
		return "OK";
	}
}
